#include "GraphicsStretchOperation.h"
#include <GraphicsBaseItem.h>
#include <GraphicsUtil.h>

GraphicsStretchOperation::GraphicsStretchOperation(GraphicsBaseItemData& itemData, QObject* parent)
	:GraphicsBaseOperation(GraphicsOperationType::StretchOperation_Enum, itemData, parent)
{

}

void GraphicsStretchOperation::stretchGraphics(GraphicsPinkItemType type, const QPointF& delta)
{
	// 考虑策略模式
	if (m_itemData.IsOriginOnCenter()) {
		// 以左上角为原点拉伸图元，TODO

	}
	else {
		// 以图元中心为原点拉伸图元
		if (GraphicsPinkItem::PinkItemType::MidRight_Enum == type) {
			stretchRight(delta);
		}
		else if (GraphicsPinkItem::PinkItemType::MidLeft_Enum == type) {
			stretchLeft(delta);
		}
		else if (GraphicsPinkItem::PinkItemType::MidTop_Enum == type) {
			stretchTop(delta);
		}
		else if (GraphicsPinkItem::PinkItemType::MidBottom_Enum == type) {
			stretchBottom(delta);
		}
	}
}

void GraphicsStretchOperation::stretchRight(const QPointF& delta)
{
	const QRectF rect = m_itemData.getScaleRect();
	const QRectF contextRect = m_itemData.getContext().rect;
	qreal newWidth = contextRect.width() + delta.x();
	if (newWidth > m_itemData.getScaleMaxSize().width()) {
		// 重新调整尺寸
		newWidth = m_itemData.getScaleMaxSize().width();
		emit sigReachMaxSize();
	}

	// 检查父图元和子图元是否碰撞
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		const QRectF childContextRect = child->getContextRect();
		// 获取子图元的外接矩形
		const QRectF childBoundingRect = GraphicsUtil::mapAnglePolygon(childContextRect, child->getItemData().getRotateAngle()).boundingRect();
		// 子图元右边与父图元右边距离
		const qreal rightDistance = newWidth - childBoundingRect.right();
		if (rightDistance < 0) {
			// 重新调整父图元的宽度，保持父图元与子图元右边界重合
			newWidth = childBoundingRect.right();
			break;
		}
	}
	QSizeF newSize(newWidth, rect.height());
	m_itemData.setScaleSize(newSize);
}

void GraphicsStretchOperation::stretchLeft(const QPointF& delta)
{
	const QRectF rect = m_itemData.getScaleRect();
	const QRectF contextRect = m_itemData.getContext().rect;
	// 实际图元位置
	QPointF newPos = contextRect.topLeft() + QPointF(delta.x(), 0);
	// 图元右下角位置
	const QPointF rightBottomPos = contextRect.bottomRight();
	// 拉伸后图元宽度
	qreal newWidth = rightBottomPos.x() - newPos.x();

	// 检查新图元的尺寸是否满足条件
	if (newWidth > m_itemData.getScaleMaxSize().width()) {
		// 重新调整新图元的尺寸
		newWidth = m_itemData.getScaleMaxSize().width();
		// 重新调整新图元的位置
		newPos.setX(contextRect.right() - newWidth);
		emit sigReachMaxSize();
	}

	QSizeF newSize(newWidth, rect.height());
	QMap<GraphicsBaseItem*, QPointF> childNewPosMap;
	//子图元和右边框保持距离不变
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		QRectF childContextRect = child->getContextRect();
		// 获取子图元的外接矩形
		const QRectF childBoundingRect = GraphicsUtil::mapAnglePolygon(childContextRect, child->getItemData().getRotateAngle()).boundingRect();
		// 子图元外接矩形中点与右边框的距离
		const qreal rightDistance = contextRect.width() - childBoundingRect.right() + childBoundingRect.width() / 2;
		qreal childXPos = newWidth - rightDistance;

		QPointF childPos = child->pos();
		// 检查父图元和子图元是否碰撞，根据子图元左部位置距离父图元左部距离是否小于0判断
		qreal leftDistance = childXPos - childBoundingRect.width() / 2;


		if (leftDistance < 0) {
			// 左部发生了碰撞,重新调整父图元的位置保持与子图元外接矩形框左部重合
			const qreal newXPos = rightBottomPos.x() - rightDistance - childBoundingRect.width() / 2;
			newPos.setX(newXPos);
			const qreal newWidth = rightDistance + childBoundingRect.width() / 2;
			newSize.setWidth(newWidth);
			childXPos = newWidth - rightDistance;
		}
		childPos.setX(childXPos);
		childNewPosMap[child] = childPos;
	}

	// 图元在x轴的移动距离
	m_itemData.setCurPos(newPos);
	m_itemData.setScaleSize(newSize);
	// 子图元的位置在父图元设置后才调整防止子图元抖动
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		const QPointF childNewPos = childNewPosMap[child];
		child->setPos(childNewPos);
	}
}

void GraphicsStretchOperation::stretchTop(const QPointF& delta, bool isUseCurrentTopLeftPos)
{
	const QRectF rect = m_itemData.getScaleRect();
	const QRectF contextRect = m_itemData.getContext().rect;

	// 实际图元位置
	QPointF newPos;
	if (isUseCurrentTopLeftPos) {
		newPos = m_itemData.getCurPos() + QPointF(0, delta.y());
	}
	else {
		newPos = contextRect.topLeft() + QPointF(0, delta.y());
	}
	// 图元的右下角位置
	const QPointF rightBottomPos = contextRect.bottomRight();
	// 拉伸后图元的高度
	qreal newHeight = contextRect.height() - delta.y();

	// 检查新图元的尺寸是否满足条件
	if (newHeight > m_itemData.getScaleMaxSize().height()) {
		// 重新调整图元高度
		newHeight = m_itemData.getScaleMaxSize().height();
		// 重新调整图元位置
		newPos.setY(contextRect.bottom() - newHeight);
		emit sigReachMaxSize();
	}
	QSizeF newSize(rect.width(), newHeight);

	// 子图元和下边框保持距离不变
	QMap<GraphicsBaseItem*, QPointF> childNewPosMap;
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		const QRectF childContextRect = child->getContextRect();
		// 获取子图元的外接矩形
		const QRectF childBoundingRect = GraphicsUtil::mapAnglePolygon(childContextRect, child->getItemData().getRotateAngle()).boundingRect();

		// 子图元外接矩形中点与父图元底部距离
		const qreal bottomDistance = contextRect.height() - childBoundingRect.bottom() + childBoundingRect.height() / 2;
		qreal childYPos = newHeight - bottomDistance;

		QPointF childPos = child->pos();
		// 检查父图元和子图元是否碰撞，根据子图元顶部位置距离父图元顶部距离是否小于0判断
		qreal topDistance = childYPos - childBoundingRect.height() / 2;
		if (topDistance < 0) {
			// 顶部发生了碰撞,重新调整父图元的位置保持与子图元外接矩形框顶部重合
			const qreal newYPos = rightBottomPos.y() - bottomDistance - childBoundingRect.height() / 2;
			newPos.setY(newYPos);
			const qreal newHeight = bottomDistance + childBoundingRect.height() / 2;
			newSize.setHeight(newHeight);
			childYPos = newHeight - bottomDistance;
		}

		childPos.setY(childYPos);
		childNewPosMap[child] = childPos;
	}
	m_itemData.setCurPos(newPos);
	m_itemData.setScaleSize(newSize);
	// 子图元的位置在父图元设置后才调整防止子图元抖动
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		const QPointF childNewPos = childNewPosMap[child];
		child->setPos(childNewPos);
	}
}

void GraphicsStretchOperation::stretchBottom(const QPointF& delta)
{
	const QRectF rect = m_itemData.getScaleRect();
	const QRectF contextRect = m_itemData.getContext().rect;
	qreal newHeight = contextRect.height() + delta.y();
	// 检查新图元的尺寸是否满足条件
	if (newHeight > m_itemData.getScaleMaxSize().height()) {
		newHeight = m_itemData.getScaleMaxSize().height();
		emit sigReachMaxSize();
	}
	// 检查父图元和子图元是否碰撞
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		const QRectF childContextRect = child->getContextRect();
		// 获取子图元的外接矩形
		const QRectF childBoundingRect = GraphicsUtil::mapAnglePolygon(childContextRect, child->getItemData().getRotateAngle()).boundingRect();
		// 子图元底部与父图元底部距离
		const qreal bottomDistance = newHeight - childBoundingRect.bottom();
		if (bottomDistance < 0) {
			// 底部发生了碰撞,重新调整父图元的高度保持与子图元底部重合
			newHeight = childBoundingRect.bottom();
			break;
		}
	}
	const QSizeF newSize(rect.width(), newHeight);
	m_itemData.setScaleSize(newSize);
}

void GraphicsStretchOperation::stretchGraphicsOnCornerKeepRatio(GraphicsPinkItemType type, const QPointF& delta)
{
	if (GraphicsPinkItem::TopLeft_Enum == type) {
		stretchKeepRatioTopLeft(delta);
	}
	else if (GraphicsPinkItem::TopRight_Enum == type) {
		stretchKeepRatioTopRight(delta);
	}
	else if (GraphicsPinkItem::BottomRight_Enum == type) {
		stretchKeepRatioBottomRight(delta);
	}
	else if (GraphicsPinkItem::BottomLeft_Enum == type) {
		stretchKeepRatioBottomLeft(delta);
	}
}

void GraphicsStretchOperation::stretchGraphicsOnCorner(GraphicsPinkItemType type, const QPointF& delta)
{
	if (GraphicsPinkItem::TopLeft_Enum == type) {
		// 拆分为左边拉伸和顶部拉伸
		stretchLeft(delta);
		stretchTop(delta, true);
	}
	else if (GraphicsPinkItem::TopRight_Enum == type) {
		// 拆分为右边拉伸和顶部拉伸
		stretchRight(delta);
		stretchTop(delta);
	}
	else if (GraphicsPinkItem::BottomRight_Enum == type) {
		// 拆分为右边拉伸和底部拉伸
		stretchRight(delta);
		stretchBottom(delta);
	}
	else if (GraphicsPinkItem::BottomLeft_Enum == type) {
		// 拆分为左边拉伸和底部拉伸
		stretchLeft(delta);
		stretchBottom(delta);
	}
}

void GraphicsStretchOperation::stretchGraphicsOnCenter(GraphicsPinkItemType type, const QPointF& delta)
{
	// 场景中图元中心
	if (m_itemData.getParentItem()) {
		m_itemData.getParentItem()->setAbsorptionLineVisable(GraphicsLineType::HorizontalEnum, false);
		m_itemData.getParentItem()->setAbsorptionLineVisable(GraphicsLineType::VerticalEnum, false);
	}

	QRectF scaleRect = m_itemData.getScaleRect();
	const QRectF contextRect = m_itemData.getContext().rect;
	const QPointF center = contextRect.center();

	// 根据对角线的计算
	const QLineF oldLine(center, m_itemData.getOperationStartMousePoint());
	const qreal oldLen = oldLine.length();
	// 当前鼠标所在的位置
	const QLineF newLine(center, m_itemData.getOperationDoingMousePoint());
	const qreal newLen = newLine.length();
	// 缩放系数
	qreal ratio = newLen / oldLen;

	// 不能进行反向缩放
	// 直线oldLine的向量
	QPointF v1 = m_itemData.getOperationStartMousePoint() - center;
	// 直线newLine的项链
	QPointF v2 = m_itemData.getOperationDoingMousePoint() - center;
	// 向量的内积
	qreal dotProduct = v1.x() * v2.x() + v1.y() * v2.y();
	// 模长
	qreal magnitudeV1 = qSqrt(v1.x() * v1.x() + v1.y() * v1.y());
	qreal magnitudeV2 = qSqrt(v2.x() * v2.x() + v2.y() * v2.y());
	qreal cosTheta = dotProduct / (magnitudeV1 * magnitudeV2);
	qreal thetaDegress = qAcos(cosTheta) * 180 / M_PI;
	if (thetaDegress > 90) {
		return;
	}

	const qreal w = contextRect.width() * ratio;
	const qreal h = contextRect.height() * ratio;
	scaleRect.setSize(QSizeF(w, h));
	// 获取外接矩形
	const QRectF boundingRect = GraphicsUtil::mapAnglePolygon(scaleRect, m_itemData.getRotateAngle()).boundingRect();

	const qreal aspectRatio = m_itemData.getBaseItemAspectRatio();
	// 检查尺寸是否满足最小尺寸
	const qreal minWidth = m_itemData.getScaleMinSize().width();
	const qreal minHeight = m_itemData.getScaleMinSize().height();

	if (minWidth > 0 && scaleRect.width() < minWidth) {
		// 根据宽度最小比例，计算高
		const qreal fitMinHeight = minWidth / aspectRatio;
		scaleRect.setWidth(minWidth);
		scaleRect.setHeight(fitMinHeight);
	}
	if (minHeight > 0 && scaleRect.height() < minHeight) {
		// 根据高度最小比例，计算宽
		const qreal fitMinWidth = minHeight * aspectRatio;
		scaleRect.setWidth(fitMinWidth);
		scaleRect.setHeight(minHeight);
	}

	// 检查尺寸是否满足最大尺寸
	const qreal maxWidth = m_itemData.getScaleMaxSize().width();
	const qreal maxHeight = m_itemData.getScaleMaxSize().height();
	if (maxWidth > 0 && scaleRect.width() > maxWidth) {
		// 根据宽度最大比例，计算高
		const qreal fitMaxHeight = maxWidth / aspectRatio;
		scaleRect.setWidth(maxWidth);
		scaleRect.setHeight(fitMaxHeight);
	}
	if (maxHeight > 0 && scaleRect.height() > maxHeight) {
		// 高度最大比例，计算宽
		const qreal fitMaxWidth = maxHeight * aspectRatio;
		scaleRect.setHeight(maxHeight);
		scaleRect.setWidth(fitMaxWidth);
	}

	// 将scaleRect的中心和原来的中心对齐
	scaleRect.moveCenter(center);

	if (!m_itemData.IsOriginOnCenter()) {
		// 检查是否超过父图元的尺寸
		if (!checkParentBounding(boundingRect, scaleRect))
			return;

		// 获取缩放图元新坐标
		const QPointF newPos = scaleRect.topLeft();
		m_itemData.setCurPos(newPos);
	}
	m_itemData.setScaleSize(scaleRect.size());
}

void GraphicsStretchOperation::stretchKeepRatioTopLeft(const QPointF& delta)
{
	QRectF contextRect = m_itemData.getContext().rect;
	const qreal maxWidth = m_itemData.getScaleMaxSize().width();
	const qreal maxHeight = m_itemData.getScaleMaxSize().height();
	// 保持右下角的点位置不变
	qreal newWidth = contextRect.width() - delta.x();
	// 拉伸后图元的高度
	qreal newHeight = contextRect.height() - delta.y();
	if (newWidth > maxWidth) {
		newWidth = maxWidth;
		emit sigReachMaxSize();
	}
	if (newHeight > maxHeight) {
		newHeight = maxHeight;
		emit sigReachMaxSize();
	}
	QSizeF newSize = GraphicsUtil::getKeepRatioSize(newWidth, newHeight, m_itemData.getFixRatio(), false);
	QMap<GraphicsBaseItem*, QPointF> childNewPosMap;
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		QRectF childRect = child->getContextRect();
		// 应该使用新位置的尺寸
		// 顶部距离
		const qreal topDistance = newSize.height() - (contextRect.height() - childRect.top());
		// 左侧距离
		const qreal leftDistance = newSize.width() - (contextRect.width() - childRect.left());
		if (leftDistance < 0) {
			newSize.setWidth(contextRect.width() - childRect.left());
		}
		if (topDistance < 0) {
			newSize.setHeight(contextRect.height() - childRect.top());
		}
		newSize = GraphicsUtil::getKeepRatioSize(newSize.width(), newSize.height(), m_itemData.getFixRatio(), true);
		// 调整子图元位置，保持子图元到父图元底部和右侧距离不变
		const qreal deltaY = newSize.height() - contextRect.height();
		const qreal deltaX = newSize.width() - contextRect.width();
		const QPointF childNewPos = child->getContext().pos + QPointF(deltaX, deltaY);
		childNewPosMap[child] = childNewPos;
	}

	// 重新调整坐标，保持右下角坐标点位置不变
	const QPointF bottomRight = contextRect.bottomRight();
	contextRect.setSize(newSize);
	contextRect.moveBottomRight(bottomRight);

	m_itemData.setCurPos(contextRect.topLeft());
	m_itemData.setScaleSize(newSize);
	// 子图元的位置在父图元设置后才调整防止子图元抖动
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		const QPointF childNewPos = childNewPosMap[child];
		child->setPos(childNewPos);
	}
}

void GraphicsStretchOperation::stretchKeepRatioTopRight(const QPointF& delta)
{
	QRectF contextRect = m_itemData.getContext().rect;
	const qreal maxWidth = m_itemData.getScaleMaxSize().width();
	const qreal maxHeight = m_itemData.getScaleMaxSize().height();
	// 保持左下角的点位置不变
	qreal newWidth = contextRect.width() + delta.x();
	// 拉伸后图元的高度
	qreal newHeight = contextRect.height() - delta.y();
	if (newWidth > maxWidth) {
		newWidth = maxWidth;
		emit sigReachMaxSize();
	}
	if (newHeight > maxHeight) {
		newHeight = maxHeight;
		emit sigReachMaxSize();
	}

	QSizeF newSize = GraphicsUtil::getKeepRatioSize(newWidth, newHeight, m_itemData.getFixRatio(), false);
	QMap<GraphicsBaseItem*, QPointF> childNewPosMap;
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		// 检查图元在新尺寸下底部位置和右边是否发生碰撞
		QRectF childRect = child->getContextRect();

		// 应该使用新位置的尺寸
		// 子图元顶部与父图元顶部距离
		const qreal topDistance = newSize.height() - (contextRect.height() - childRect.top());
		// 子图元右侧与父图元右侧距离
		const qreal rightDistance = newSize.width() - childRect.right();
		if (topDistance < 0) {
			// 调整父图元顶部与子图元顶部重合
			newSize.setHeight(contextRect.height() - childRect.top());
		}
		if (rightDistance < 0) {
			// 调整父图元右部与子图元右部重合
			newSize.setWidth(childRect.right());
		}
		newSize = GraphicsUtil::getKeepRatioSize(newSize.width(), newSize.height(), m_itemData.getFixRatio(), true);
		// 调整子图元位置，保持子图元左侧，底部到父图元的左侧和底部距离不变
		const qreal deltaY = newSize.height() - contextRect.height();
		const QPointF newChildPos = child->getContext().pos + QPointF(0, deltaY);
		childNewPosMap[child] = newChildPos;
	}
	// 重新调整坐标，保持左下角坐标点位置不变
	const QPointF bottomLeft = contextRect.bottomLeft();
	contextRect.setSize(newSize);
	contextRect.moveBottomLeft(bottomLeft);
	m_itemData.setCurPos(contextRect.topLeft());
	m_itemData.setScaleSize(newSize);
	// 子图元的位置在父图元设置后才调整防止子图元抖动
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		const QPointF childNewPos = childNewPosMap[child];
		child->setPos(childNewPos);
	}
}

void GraphicsStretchOperation::stretchKeepRatioBottomRight(const QPointF& delta)
{
	QRectF contextRect = m_itemData.getContext().rect;
	const qreal maxWidth = m_itemData.getScaleMaxSize().width();
	const qreal maxHeight = m_itemData.getScaleMaxSize().height();
	qreal newWidth = contextRect.width() + delta.x();
	qreal newHeight = contextRect.height() + delta.y();
	if (newWidth > maxWidth) {
		newWidth = maxWidth;
		emit sigReachMaxSize();
	}
	if (newHeight > maxHeight) {
		newHeight = maxHeight;
		emit sigReachMaxSize();
	}

	QSizeF newSize = GraphicsUtil::getKeepRatioSize(newWidth, newHeight, m_itemData.getFixRatio(), false);
	// 右边拉伸，左上角保持不变
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		// 检查图元在新尺寸下底部位置和右边是否发生碰撞
		QRectF childRect = child->getContextRect();
		// 底部距离
		const qreal bottomDistance = newSize.height() - childRect.bottom();
		// 右边距离
		const qreal rightDistance = newSize.width() - childRect.right();
		if (bottomDistance < 0 && rightDistance < 0) {
			// 底部和右侧发生碰撞，调整父图元底部，右侧位置与子图元底部，右侧重合
			newSize.setWidth(childRect.right());
			newSize.setHeight(childRect.bottom());
		}
		else if (bottomDistance < 0) {
			// 底部发生碰撞,调整父图元底部位置与子图元底部重合
			newSize.setHeight(childRect.bottom());
		}
		else if (rightDistance < 0) {
			// 右侧发生碰撞,调整父图元右侧位置与子图元右侧重合
			newSize.setWidth(childRect.right());
		}
		newSize = GraphicsUtil::getKeepRatioSize(newSize.width(), newSize.height(), m_itemData.getFixRatio(), true);
	}
	m_itemData.setScaleSize(newSize);
}

void GraphicsStretchOperation::stretchKeepRatioBottomLeft(const QPointF& delta)
{
	QRectF contextRect = m_itemData.getContext().rect;
	const qreal maxWidth = m_itemData.getScaleMaxSize().width();
	const qreal maxHeight = m_itemData.getScaleMaxSize().height();
	// 保持左下角的点位置不变
	qreal newWidth = contextRect.width() - delta.x();
	// 拉伸后图元的高度
	qreal newHeight = contextRect.height() + delta.y();
	if (newWidth > maxWidth) {
		newWidth = maxWidth;
		emit sigReachMaxSize();
	}
	if (newHeight > maxHeight) {
		newHeight = maxHeight;
		emit sigReachMaxSize();
	}
	QSizeF newSize = GraphicsUtil::getKeepRatioSize(newWidth, newHeight, m_itemData.getFixRatio(), false);
	QMap<GraphicsBaseItem*, QPointF> childNewPosMap;
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		QRectF childRect = child->getContextRect();
		// 应该使用新位置的尺寸
		// 子图元左侧距离父图元左侧的距离
		const qreal leftDistance = newSize.width() - (contextRect.right() - childRect.left());
		// 子图元底部距离父图元底部的距离
		const qreal bottomDistance = newSize.height() - childRect.bottom();

		if (leftDistance < 0) {
			newSize.setWidth(contextRect.width() - childRect.left());
		}
		if (bottomDistance < 0) {
			newSize.setHeight(childRect.bottom());
		}
		newSize = GraphicsUtil::getKeepRatioSize(newSize.width(), newSize.height(), m_itemData.getFixRatio(), true);
		// 调整子图元位置，保持子图元右侧到父图元右侧距离不变
		const qreal deltaX = newSize.width() - contextRect.width();
		const QPointF newChildPos = child->getContext().pos + QPointF(deltaX, 0);
		childNewPosMap[child] = newChildPos;
	}
	// 重新调整坐标，保持右上角坐标点位置不变
	const QPointF topRight = contextRect.topRight();
	contextRect.setSize(newSize);
	contextRect.moveTopRight(topRight);
	m_itemData.setCurPos(contextRect.topLeft());
	m_itemData.setScaleSize(newSize);
	// 子图元的位置在父图元设置后才调整防止子图元抖动
	for (GraphicsBaseItem* child : m_itemData.getChilds()) {
		const QPointF childNewPos = childNewPosMap[child];
		child->setPos(childNewPos);
	}
}

bool GraphicsStretchOperation::checkParentBounding(const QRectF& boundingRect, QRectF& scaleRect)
{
	if (m_itemData.getParentItem()) {
		const QRectF parentRect = m_itemData.getParentItem()->getItemData().getScaleRect();
		if (scaleRect.width() > parentRect.width())
		{
			// 设置子图元宽为父图元宽
			scaleRect.setWidth(parentRect.width());
			const qreal height = parentRect.width() / m_itemData.getBaseItemAspectRatio();
			// 检查高
			if (height > parentRect.height())
				return false;
			scaleRect.setHeight(height);
			scaleRect.moveLeft(-scaleRect.width() / 2);
			const QPointF newPos = scaleRect.center();
			m_itemData.setScaleSize(scaleRect.size());
			m_itemData.setCurPos(newPos);
			return false;
		}

		if (scaleRect.height() > parentRect.height())
		{
			//设置子图元高为父图元高
			scaleRect.setHeight(parentRect.height());
			const qreal width = scaleRect.height() * m_itemData.getBaseItemAspectRatio();
			//  检查宽
			if (width > parentRect.width())
				return false;
			scaleRect.setWidth(width);
			scaleRect.moveTop(0);

			const QPointF newPos = scaleRect.center();
			m_itemData.setScaleSize(scaleRect.size());
			m_itemData.setCurPos(newPos);
			return false;
		}


		// 检查底部是否越界
		if (boundingRect.bottom() > parentRect.height())
		{
			// 和父图元底部重合,重新调整子图元坐标，并检查图元是否越界
			scaleRect.moveBottom(parentRect.height());
			const QPointF newPos = scaleRect.center();
			m_itemData.setScaleSize(scaleRect.size());
			m_itemData.setCurPos(newPos);
			return false;
		}

		// 检查顶部是否越界
		if (boundingRect.top() < 0) {
			// 和父图元顶部重合,重新调整子图元坐标，并检查图元是否越界
			scaleRect.moveTop(0);
			const QPointF newPos = scaleRect.center();
			m_itemData.setScaleSize(scaleRect.size());
			m_itemData.setCurPos(newPos);
			return false;
		}


		// 检查左边是否越界
		if (boundingRect.left() < 0) {
			// 和父图元左边重合，重新调整子图元坐标，并检查图元是否越界
			scaleRect.moveLeft(0);
			const QPointF newPos = scaleRect.center();
			m_itemData.setScaleSize(scaleRect.size());
			m_itemData.setCurPos(newPos);
			return false;
		}

		// 检查右边是否越界
		if (boundingRect.right() > parentRect.width()) {
			// 和父图元右边重合,重新调整子图元坐标，并检查图元是否越界
			scaleRect.moveRight(parentRect.width());
			const QPointF newPos = scaleRect.center();
			m_itemData.setScaleSize(scaleRect.size());
			m_itemData.setCurPos(newPos);
			return false;
		}
	}
	return true;
}
